package com.ymatou.iapi.optrade.testcase;

import java.util.HashMap;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.optrade.parameter.req.RiskControlNotifyReq;
import com.ymatou.iapi.optrade.resp.TradingServiceResp;
import com.ymatou.iapi.optrade.service.RiskControlNotifyCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.YMTTrading.TradingCallService;
import com.ymttest.database.sharding.db.sharding.model.OrderPo;
import com.ymttest.database.sharding.db.sqlwapper.OrderShardingWapper;
/**
*风控检测通知交易
*FreeMaker Template自动生成代码
*/
@TestSuite
@Ignore
//消息总线配上后，没发直接通过接口测试
public class Ts_RiskControlNotify {
	private static RiskControlNotifyReq riskControlNotifyReq;
	private static RiskControlNotifyCall riskControlNotifyCall;
	
	private static OrderShardingWapper orderShardingWapper = new OrderShardingWapper();
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("风控检测通知交易");
	}

	@Before
	public void caseUp() {
		riskControlNotifyReq = new RiskControlNotifyReq();
		riskControlNotifyCall = new RiskControlNotifyCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(riskControlNotifyCall.getOpurl());
		orderShardingWapper.activeDeadCommunicationsLink(orderShardingWapper);
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_RiskControlNotify_001() {
		Logger.start(true, "订单支付后,风控通知交易状态为pass");

		try {
			TradingServiceResp placeOrderResp = TradingCallService.payOrder();

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();

			Map<String, String> paramExt = new HashMap<String, String>();

			paramExt.put("Status", "pass");

			riskControlNotifyReq.setParamExt(paramExt);
			riskControlNotifyReq.setOrderId(orderId);

			riskControlNotifyCall.setData(riskControlNotifyReq);

			riskControlNotifyCall.callService();

			Logger.verifyEquals("ok", riskControlNotifyCall.getReturnData(),
					"验证Code");

			OrderPo orderPo = orderShardingWapper
					.selectOrderbyOrderId(orderId);

			Logger.verifyEquals(2, orderPo.getRiskStatus(), "校验riskStatus");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_RiskControlNotify_002() {
		Logger.start(true, "订单支付后,风控通知交易状态为Rejected,交易取消订单");

		try {
			TradingServiceResp placeOrderResp = TradingCallService.payOrder();

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();

			Map<String, String> paramExt = new HashMap<String, String>();

			paramExt.put("Status", "Rejected");

			riskControlNotifyReq.setParamExt(paramExt);
			riskControlNotifyReq.setOrderId(orderId);

			riskControlNotifyCall.setData(riskControlNotifyReq);

			riskControlNotifyCall.callService();

			Logger.verifyEquals("ok", riskControlNotifyCall.getReturnData(),
					"验证Code");

			Thread.sleep(2000);

			OrderPo orderPo = orderShardingWapper
					.selectOrderbyOrderId(orderId);

			Logger.verifyEquals(18, orderPo.getOrderStatus(), "校验orderStatus");
			Logger.verifyEquals(500000, orderPo.getOrderStatusMemo(),
					"校验orderStatusMemo");

			Logger.verifyEquals(-1, orderPo.getRiskStatus(), "校验riskStatus");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}